package com.ruoyi.paper.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.paper.domain.QtPaperTkNums;
import com.ruoyi.paper.domain.QtTkPaper;
import com.ruoyi.paper.mapper.QtPaperTkNumsMapper;
import com.ruoyi.paper.mapper.QtTkPaperMapper;
import com.ruoyi.system.domain.QtOption;
import com.ruoyi.system.domain.QtTk;
import com.ruoyi.system.mapper.QtOptionMapper;
import com.ruoyi.system.mapper.QtTkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.paper.mapper.QtPaperMapper;
import com.ruoyi.paper.domain.QtPaper;
import com.ruoyi.paper.service.IQtPaperService;
import org.springframework.transaction.annotation.Transactional;

/**
 * paperService业务层处理
 * 
 * @author zqq
 * @date 2021-11-18
 */
@Service
public class QtPaperServiceImpl implements IQtPaperService 
{
    @Autowired
    private QtPaperMapper qtPaperMapper;

    @Autowired
    private QtPaperTkNumsMapper qtPaperTkNumsMapper;

    @Autowired
    private QtTkMapper qtTkMapper;
    @Autowired
    private QtTkPaperMapper qtTkPaperMapper;
    @Autowired
    private QtOptionMapper qtOptionMapper;
    /**
     * 查询paper
     * 
     * @param paperId paper主键
     * @return paper
     */
    @Override
    public QtPaper selectQtPaperByPaperId(String paperId)
    {
        return qtPaperMapper.selectQtPaperByPaperId(paperId);
    }

    /**
     * 查询paper列表
     * 
     * @param qtPaper paper
     * @return paper
     */
    @Override
    public List<QtPaper> selectQtPaperList(QtPaper qtPaper)
    {
        return qtPaperMapper.selectQtPaperList(qtPaper);
    }

    /**
     * 新增paper
     * 
     * @param qtPaper paper
     * @return 结果
     */
    @Override
    @Transactional
    public int insertQtPaper(QtPaper qtPaper)
    {
        String id=IdUtils.randomUUID();
        qtPaper.setPaperId(id);
        List<QtPaperTkNums> tkNumsList=qtPaper.getTkNumsList();
        for(QtPaperTkNums item:tkNumsList){
            item.setPaperId(id);
            qtPaperTkNumsMapper.insertQtPaperTkNums(item);
        }
        return qtPaperMapper.insertQtPaper(qtPaper);
    }

    /**
     * 修改paper
     * 
     * @param qtPaper paper
     * @return 结果
     */
    @Override
    public int updateQtPaper(QtPaper qtPaper)
    {
        List<QtPaperTkNums> tkNumsList=qtPaper.getTkNumsList();
        for(QtPaperTkNums item:tkNumsList){
            qtPaperTkNumsMapper.updateQtPaperTkNums(item);
        }
        return qtPaperMapper.updateQtPaper(qtPaper);
    }

    /**
     * 批量删除paper
     * 
     * @param paperIds 需要删除的paper主键
     * @return 结果
     */
    @Override
    public int deleteQtPaperByPaperIds(String[] paperIds)
    {
        return qtPaperMapper.deleteQtPaperByPaperIds(paperIds);
    }

    /**
     * 删除paper信息
     * 
     * @param paperId paper主键
     * @return 结果
     */
    @Override
    public int deleteQtPaperByPaperId(String paperId)
    {
        return qtPaperMapper.deleteQtPaperByPaperId(paperId);
    }

    /**
     * 自动组卷
     *
     * @param qtPaper
     * @return
     */
    @Transactional
    public int zujuan(QtPaper qtPaper) {
        try {
            QtPaperTkNums qtPaperTkNums=new QtPaperTkNums();
            qtPaperTkNums.setPaperId(qtPaper.getPaperId());
            //按试卷id获取到题型数量集合
            List<QtPaperTkNums> tkNumsList=qtPaperTkNumsMapper.selectQtPaperTkNumsList(qtPaperTkNums);
            List<String> list=new ArrayList<>();
            System.out.println("ddd"+tkNumsList);
            for(QtPaperTkNums tkNums : tkNumsList){
                String n=tkNums.getPaperNum()==null?"0":tkNums.getPaperNum();
                qtPaper.setPaperInt(Integer.valueOf(n));
                qtPaper.setPaperStr(tkNums.getTkTypeId());
                list.addAll(qtTkMapper.getTkId(qtPaper));
            }
//            System.out.println(list==null);
            QtTkPaper qtTkPaper=new QtTkPaper();
            qtTkPaper.setPaperId(qtPaper.getPaperId());

            for(String item:list){
                qtTkPaper.setTkId(item);
                qtTkPaperMapper.insertQtTkPaper(qtTkPaper);
            }
            return 1;
        }catch (Exception e) {
            return 0;
        }

    }

    @Override
    public List<QtTk> getTk(QtPaper qtPaper) {
        QtTkPaper qtTkPaper=new QtTkPaper();
        qtTkPaper.setPaperId(qtPaper.getPaperId());
        List<QtTkPaper> list=qtTkPaperMapper.selectQtTkPaperList(qtTkPaper);
        StringBuilder tkStr=new StringBuilder();
        tkStr.append("(");
        for (QtTkPaper item : list) {
            tkStr.append("'"+item.getTkId()+"',");
        }
        tkStr.deleteCharAt(tkStr.length()-1);
        tkStr.append(")");
        QtTk tk=new QtTk();
        tk.setTkStr(tkStr.toString());
        List<QtTk> list1 = qtTkMapper.selectQtTkList(tk);
        QtOption option=new QtOption();
        for(QtTk item:list1){
            option.setTkId(item.getTkId());
            item.setOptions(qtOptionMapper.selectQtOptionList(option));
        }
        return list1;
    }
}
